package app

import (
	"context"
	"net/http"
	"awakengo/internal/service"
	"awakengo/internal/wake"
)

type App struct {
	server  *http.Server
	service *service.SSEService
}

func NewApp() *App {
	sseService := service.NewSSEService()
	
	app := &App{
		service: sseService,
		server: &http.Server{
			Addr: ":8080",
		},
	}

	// 启动语音唤醒服务
	err := wake.StartWakeService(func(msg string) {
		app.service.NotifyWakeup(msg)
	})
	if err != nil {
		panic(err)
	}

	return app
}

func (a *App) Run() error {
	mux := http.NewServeMux()
	
	// SSE事件流端点
	mux.HandleFunc("GET /events", a.service.HandleSSE)
	
	a.server.Handler = mux
	return a.server.ListenAndServe()
}

func (a *App) Shutdown(ctx context.Context) error {
	// 停止语音唤醒服务
	wake.StopWakeService()
	return a.server.Shutdown(ctx)
} 